<?php

namespace Services\RabbitMq;

use PhpAmqpLib\Connection\AMQPStreamConnection;

class RabbitMqManager
{
    protected static $_instance;

    private function __construct($host, $port, $user, $password)
    {
        $this->host = $host;
        $this->port = $port;
        $this->user = $user;
        $this->password = $password;
        $this->createConnection($this->host, $this->port, $this->user, $this->password);
    }

    public static function getInstance($host, $port, $user, $password)
    {
        if (self::$_instance === null) {
            self::$_instance = new self($host, $port, $user, $password);
        }

        return self::$_instance;
    }

    private function __clone()
    {
    }

    public function __wakeup()
    {
    }

    public function createConnection($host, $port, $user, $password)
    {
        $this->connection = new AMQPStreamConnection($host, $port, $user, $password);
        $this->channel = $this->connection->channel();
    }

    public function closeConnection()
    {
        $this->connection->close();
        $this->channel->close();
    }

    public function createQueue($queueName)
    {
        $this->channel->queue_declare($queueName, false, true, false, false);
    }
}
